<?php
/**
 * Created by PhpStorm.
 * User: 17828
 * Date: 2018/10/10
 * Time: 10:52
 */
namespace app\common\model;
use think\Model;
class Car extends Model
{
    public function getLocationStoreCar($field,$where,$page,$limit){
        $list['data'] = db('car')->alias('c')
            ->field($field)
            ->join('cartype ca','ca.cartype_id = c.cartype_id')
            ->join('cartype ct','ct.cartype_id = c.cartype_ids')
            ->join('store s','s.store_id = c.store_id')
            ->where($where)
            ->page($page,$limit)
            ->group('c.car_id')
            ->order('c.car_id desc')
            ->select();

        $list['count'] = db('car')->alias('c')
            ->field($field)
            ->join('cartype ca','c.cartype_id = ca.cartype_id')
            ->join('cartype ct','c.cartype_ids = ct.cartype_id')
            ->join('store s','s.store_id = c.store_id')
            ->where($where)
            ->page($page,$limit)
            ->group('c.car_id')
            ->select();
        return $list;
    }

    public function getAcceptTransfer($field,$where,$page,$limit){
        $list['data'] = db('allocation')->alias('a')
            ->field($field)
            ->join('car c','c.car_id = a.car_id')
            ->join('cartype ca','c.cartype_id = ca.cartype_id')
            ->join('cartype ct','c.cartype_ids = ct.cartype_id')
            ->join('store s','s.store_id = a.call_store_id','left')
            ->join('store st','st.store_id = a.accept_store_id')
            ->join('store sto','sto.store_id = c.store_id')
            ->where($where)
            ->page($page,$limit)
            ->group('a.allocation_id')
            ->order('a.allocation_id desc')
            ->select();
        $list['count'] = db('allocation')->alias('a')
            ->field($field)
            ->join('car c','c.car_id = a.car_id')
            ->join('cartype ca','c.cartype_id = ca.cartype_id')
            ->join('cartype ct','c.cartype_ids = ct.cartype_id')
            ->join('store s','s.store_id = a.call_store_id','left')
            ->join('store st','st.store_id = a.accept_store_id')
            ->join('store sto','sto.store_id = c.store_id')
            ->where($where)
            ->group('a.allocation_id')
            ->count();
        return $list;
    }
    public function getCarBalance($field,$where,$page, $limit){
        $data = db('car')->alias('c')
            ->field($field)
            ->join('contractcar co', 'co.car_id = c.car_id')
            ->where($where)
            ->group('c.car_id')
            ->page($page, $limit)
            ->select();
        $count = db('car')->alias('c')
            ->field($field)
            ->join('contractcar co', 'co.car_id = c.car_id')
            ->where($where)
            ->group('c.car_id')
            ->count();
        return ['data'=>$data,'count'=>$count];
    }
}